<?php
namespace Home\Controller;
use Common\Controller\BaseController;
use Think\Controller;
class CommentController extends BaseController {
    public function index(){
        if(!IS_GET){
            $this->error('非法访问');
        }
        $drug_id = I('get.id');
        if(empty($drug_id)){
            $this->error('没有找到该药品');
        }
        $this->assign('comment_drug',$drug_id);
        $this->display('comment');
    }


    public function comment() {
        if(!IS_POST){
            $this->error('非法访问');
        }
        $drug_id = I('get.id');
        if(empty($drug_id)){
            $this->error('没有找到该药品');
        }
        if(!is_login()){
            $this->error('评论需要登录，请登录');
        }
        $userId = $_SESSION['user_login']['objectId'];
        if(empty($userId)){
            $this->error('登录信息失效，请重新登录');
        }
        //获取评论相关的信息
        $data['cure'] = intval(I('post.cure'));
        $data['package'] = intval(I('post.package'));
        $data['buy'] = intval(I('post.buy'));
        $data['use'] = intval(I('post.use'));
        $data['price'] = intval(I('post.price'));
        $data['side_effect'] = intval(I('post.side_effect'));

        $data['drugstore'] = I('post.drugstore');
        $data['drug_price'] = I('post.drug_price');

        $data['content'] = trim(I('post.content'));
        if(mb_strlen($data['content']) < 30 || mb_strlen($data['content']) > 500){
            $this->error('评论字数不能少于30字多于500字');
        }

        $data['user'] = $userId;
        $data['drug'] = $drug_id;

        $comment_res = json_decode($this->http->curl_request(BASE_URL.'comment',$data),true);
        if(isset($comment_res['code'])) {
            if($comment_res['code'] == 200) {
                $this->success('评论成功',U('Home/Drug/index',array('id'=>$drug_id)));
            }else{
                $this->error($comment_res['message']);
            }
        }else{
            $this->error('网络异常，请稍后重试');
        }

    }
}